import { _decorator, Component, Node, Vec3 } from 'cc';
import { AnimatorComp } from '../comp/AnimatorComp';
import { BaseStateMachine } from './fsm/BaseStateMachine';
const { ccclass, property } = _decorator;

@ccclass('BaseSprite')
export class BaseSprite extends Component {
    protected _render: Node = null;
    protected _animator: AnimatorComp = null;
    protected _fsm: BaseStateMachine = null;

    onLoad() {
        this._animator = this.node.getChildByName("render").getComponent(AnimatorComp);
    }

    update(deltaTime: number) {
        if (this._fsm && this._fsm.isOn) {
            this._fsm.onUpdate(deltaTime);
        }
    }

    public get render() {
        return this._render;
    }

    public initFSM(sm : BaseStateMachine){
        this._fsm = sm;
    }

    public simpleMove(direction : Vec3) {
        let nowPos = this.node.getPosition();
        let targetPos = nowPos.add(direction);
        this.node.setPosition(targetPos);
    }

    public playAnimation(animName : string, speed : number = 1, loop : boolean = false) {
        if (this._animator) {
            this._animator.play(animName, speed, loop);
        }
    }
}


